package com.dns.customers.dao.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.dns.customers.DnsTask;
import com.dns.customers.dao.DomainDao;
import com.dns.customers.model.*;
import com.dns.framework.data.IDataset;
import com.dns.framework.page.Pagination;
import com.util.DataSourceUtil;
import com.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.servlet.http.HttpServletRequest;

import java.util.*;

/**
 * @author :  yulei
 * @data :  2018/5/29 10:57
 * @Version :  1.0
 **/

@Repository
public class DomainDaoImpl  extends DataSourceUtil implements DomainDao  {

    @Autowired
    HttpServletRequest request;

    /**
     * 修改跟域名
     * @param custId 客户id
     * @param rootDomains 域名集合
     * @return
     * @throws Exception
     */
    @Override
    public String custDomain(String custId,List<Map<String,Object>> rootDomains) throws Exception{
        String logId = getLogId();
        Date now = DateUtil.getCurrentTime();

        String insertInfoSql="insert into dns_domain_info(CUST_ID,DOMAIN_TYPE,DOMAIN_NAME,UPDATE_TIME,REAMRK) " +
                " values (:CUST_ID,:DOMAIN_TYPE,:DOMAIN_NAME,:UPDATE_TIME,:REMARK)";
        String insetInfoLogSql = "INSERT INTO dns_domain_info_log (LOG_ID,CUST_ID,OPERATE_TYPE,DOMAIN_TYPE,DOMAIN_NAME,UPDATE_TIME,REAMRK)" +
                " values (:LOG_ID,:CUST_ID,:OPERATE_TYPE,:DOMAIN_TYPE,:DOMAIN_NAME,:UPDATE_TIME,:REMARK)";
        String queryInfoSql = "select DOMAIN_NAME from dns_domain_info where CUST_ID=:CUST_ID and DOMAIN_NAME=:DOMAIN_NAME and DOMAIN_TYPE=DOMAIN_TYPE";
        String querySql = "select DOMAIN_NAME from dns_domain_record where DOMAIN_NAME=:DOMAIN_NAME and DOMAIN_TYPE=DOMAIN_TYPE and TYPE=:TYPE";

        String insetSql = "INSERT INTO dns_domain_record(DOMAIN_NAME,DOMAIN_TYPE,TYPE,UPDATE_TIME,REMARK)"+
                " values (:DOMAIN_NAME,:DOMAIN_TYPE,:TYPE,:UPDATE_TIME,:REMARK)";
        String insetLogSql = "INSERT INTO dns_domain_record_log (LOG_ID,DOMAIN_NAME,DOMAIN_TYPE,OPERATE_TYPE,TYPE,UPDATE_TIME,REMARK)" +
                " values (:LOG_ID,:DOMAIN_NAME,:DOMAIN_TYPE,:OPERATE_TYPE,:TYPE,:UPDATE_TIME,:REMARK);";
        for (Map<String,Object> domain : rootDomains){
            MapSqlParameterSource params = new MapSqlParameterSource();
            String remark = "null".equals(domain.get("remark")+"")?"":domain.get("remark")+"";
            params.addValue("LOG_ID",logId);
            params.addValue("CUST_ID",custId);
            params.addValue("DOMAIN_NAME",domain.get("domainName"));
            params.addValue("DOMAIN_TYPE",domain.get("domain_type"));
            params.addValue("OPERATE_TYPE",domain.get("添加域名信息"));
            params.addValue("REMARK",remark);
            params.addValue("UPDATE_TIME",now);
            List list = daoManager.queryForList(queryInfoSql.toString(),params);
            if (list==null || list.size() <= 0){
                return "不能添加根域名";
            }
            List<String> types = (List<String>) domain.get("type");
            for (String type : types){
                params.addValue("TYPE",type);
                List typelist = daoManager.queryForList(querySql.toString(),params);
                if (typelist == null || typelist.size()<=0){
                    daoManager.update(insetSql.toString(),params);
//                    daoManager.update(insetLogSql.toString(),params);
                }
            }
        }
        return "保存成功";
    }

    /**
     * 新增或修改子域名
     * @param custId
     * @param childDomainMap
     * @return
     * @throws Exception
     */
    @Override
    public String custChildDomain(String custId,Map<String,List<Map<String,Object>>> childDomainMap) throws Exception{
        String logId = getLogId();
        Date now = DateUtil.getCurrentTime();
        String insertInfoSql="insert into dns_domain_info(CUST_ID,DOMAIN_TYPE,DOMAIN_NAME,PARENT_DOMAIN_NAME,UPDATE_TIME,REAMRK) " +
                " values (:CUST_ID,:DOMAIN_TYPE,:DOMAIN_NAME,:PARENT_DOMAIN_NAME,:UPDATE_TIME,:REMARK)";
        String insetInfoLogSql = "INSERT INTO dns_domain_info_log (LOG_ID,CUST_ID,OPERATE_TYPE,DOMAIN_TYPE,DOMAIN_NAME,PARENT_DOMAIN_NAME,UPDATE_TIME,REAMRK)" +
                " values (:LOG_ID,:CUST_ID,:OPERATE_TYPE,:DOMAIN_TYPE,:DOMAIN_NAME,:PARENT_DOMAIN_NAME,:UPDATE_TIME,:REMARK);";
        String queryInfoSql = "select DOMAIN_NAME from dns_domain_info where CUST_ID=:CUST_ID and DOMAIN_NAME=:DOMAIN_NAME and DOMAIN_TYPE=DOMAIN_TYPE";
        String querySql = "select DOMAIN_NAME from dns_domain_record where DOMAIN_NAME=:DOMAIN_NAME and DOMAIN_TYPE=DOMAIN_TYPE and TYPE=:TYPE";

        String insetSql = "INSERT INTO dns_domain_record(DOMAIN_NAME,DOMAIN_TYPE,TYPE,UPDATE_TIME,REMARK)"+
                " values (:DOMAIN_NAME,:DOMAIN_TYPE,:TYPE,:UPDATE_TIME,:REMARK)";
        String insetLogSql = "INSERT INTO dns_domain_record_log (LOG_ID,DOMAIN_NAME,DOMAIN_TYPE,OPERATE_TYPE,TYPE,UPDATE_TIME,REMARK)" +
                " values (:LOG_ID,:DOMAIN_NAME,:DOMAIN_TYPE,:OPERATE_TYPE,:TYPE,:UPDATE_TIME,:REMARK);";
        for (Map.Entry<String,List<Map<String,Object>>> entry : childDomainMap.entrySet()) {
            List<Map<String, Object>> rootDomains = entry.getValue();
            for (Map<String,Object> domain : rootDomains){
                MapSqlParameterSource params = new MapSqlParameterSource();
                String remark = "null".equals(domain.get("remark")+"")?"":domain.get("remark")+"";
                params.addValue("LOG_ID",logId);
                params.addValue("CUST_ID",custId);
                params.addValue("DOMAIN_NAME",domain.get("domainName"));
                params.addValue("DOMAIN_TYPE",domain.get("domain_type"));
                params.addValue("PARENT_DOMAIN_NAME",domain.get("parent_domain"));
                params.addValue("OPERATE_TYPE",domain.get("添加域名信息"));
                params.addValue("REMARK",remark);
                params.addValue("UPDATE_TIME",now);
                List list = daoManager.queryForList(queryInfoSql.toString(),params);
                if (list==null || list.size() <= 0){
                    daoManager.update(insertInfoSql.toString(),params);
                    daoManager.update(insetInfoLogSql.toString(),params);
                }
                List<String> types = (List<String>) domain.get("type");
                for (String type : types){
                    params.addValue("TYPE",type);
                    List typelist = daoManager.queryForList(querySql.toString(),params);
                    if (typelist == null || typelist.size()<=0){
                        daoManager.update(insetSql.toString(),params);
                        daoManager.update(insetLogSql.toString(),params);
                    }
                }
            }
        }
        return "保存成功";
    }
    /**
     * 根据域名名称和等级查询域名的所有类型
     * @return
     */
    public List<String> checkDomainTypes(String domainName,String domainType){
        List<String> result = null;
        try{
            //查询sql
            StringBuffer querySql = new StringBuffer();
            querySql.append("select DOMAIN_NAME,TYPE from dns_domain_record " +
                    "where DOMAIN_NAME=:DOMAIN_NAME AND DOMAIN_TYPE=:DOMAIN_TYPE");
            MapSqlParameterSource param = new MapSqlParameterSource();
            param.addValue("DOMAIN_NAME",domainName);
            param.addValue("DOMAIN_TYPE",domainType);
            List<Map<String,Object>> records = daoManager.queryForList(querySql.toString(),param);
            if (records != null && records.size()>0){
                result = new ArrayList<>();
                for(Map<String,Object> rd : records){
                    String type = (String)rd.get("TYPE");
                    if (type != null){
                        result.add(type);
                    }
                }
            }
        }catch (Exception e){
            result = null;
        }
        return result;
    }

    /**
     * 批量删除根域名记录信息
     * @param cust
     * @return
     * @throws Exception
     */
    @Override
    public String delDomainRecordList(String cust)  throws  Exception{
        String logId = getLogId();
        StringBuffer msg = new StringBuffer();
        try{
            JSONObject queryDnsResultRequest = JSON.parseObject(cust);
            JSONObject customerJSON = queryDnsResultRequest.getJSONObject("customers");
            JSONObject userJSON = customerJSON.getJSONObject("user");
            JSONObject cusdomainJSON = customerJSON.getJSONObject("customerDomain");
            JSONArray domains = cusdomainJSON.getJSONArray("rootDomain");
            //查询sql
            StringBuffer querySql = new StringBuffer();
            querySql.append("select DOMAIN_NAME,TYPE from dns_domain_record " +
                    "where DOMAIN_NAME=:DOMAIN_NAME AND DOMAIN_TYPE=:DOMAIN_TYPE");
            //删除sql
            StringBuffer delsql = new StringBuffer();
            delsql.append("delete from dns_domain_record " +
                    "where DOMAIN_NAME=:DOMAIN_NAME AND DOMAIN_TYPE=:DOMAIN_TYPE and TYPE=:TYPE");
            //日志sql
            StringBuffer insetDomainLog = new StringBuffer();
            insetDomainLog.append("INSERT INTO dns_domain_record_log (LOG_ID,DOMAIN_NAME,DOMAIN_TYPE,OPERATE_TYPE,UPDATE_TIME,REMARK)");
            insetDomainLog.append(" values (:LOG_ID,:DOMAIN_NAME,:DOMAIN_TYPE,:OPERATE_TYPE,:UPDATE_TIME,:REMARK)");

            MapSqlParameterSource param =null;
            for(int i=0;i<domains.size();i++){
                JSONObject domain = domains.getJSONObject(i);
                param = new MapSqlParameterSource();
                param.addValue("DOMAIN_NAME",domain.get("domainName"));
                param.addValue("DOMAIN_TYPE",domain.get("domainType"));
                JSONArray types = domain.getJSONArray("record");
                for (int j=0;j<types.size();j++){
                    param.addValue("TYPE",types.getJSONObject(j).get("type"));
                    int a = daoManager.update(delsql.toString(),param);
                    if (a > 0 ){
                        //记录日志
                        param.addValue("LOG_ID", logId);
                        param.addValue("OPERATE_TYPE", "D");
                        param.addValue("UPDATE_TIME",DateUtil.getCurrentTime() );
                        param.addValue("REMARK","批量删除域名为："+domain.get("domainName")+",TYPE为："+domain.get("type")+"的记录信息");
                        daoManager.update(insetDomainLog.toString(), param);
                    }
                }
                //日志记录需要先查询出来。以便获取数据
                List<Map<String,Object>> domtypeList = daoManager.queryForList(querySql.toString(),param);



            }
        }catch(Exception e){
            e.printStackTrace();
            return "删除异常";
        }
        return "删除成功";
    }

    /**
     * 批量删除子域名信息
     * @param cust
     * @return
     * @throws Exception
     */
    @Override
    public String delCustomerConfig(String cust)  throws  Exception{
        String logId = getLogId();
        StringBuffer msg = new StringBuffer();
        try{
            JSONObject queryDnsResultRequest = JSON.parseObject(cust);
            JSONObject customerJSON = queryDnsResultRequest.getJSONObject("customers");
            JSONObject userJSON = customerJSON.getJSONObject("user");
            JSONObject cusdomainJSON = customerJSON.getJSONObject("customerDomain");
            JSONArray domains = cusdomainJSON.getJSONArray("delDomain");
            //查询sql
            StringBuffer querySql = new StringBuffer();
            querySql.append("select DOMAIN_NAME,PARENT_DOMAIN_NAME from dns_domain_info " +
                    "where DOMAIN_NAME=:DOMAIN_NAME AND DOMAIN_TYPE=:DOMAIN_TYPE");
            StringBuffer querySql2 = new StringBuffer();
            querySql2.append("select DOMAIN_NAME,TYPE from dns_domain_record " +
                    "where DOMAIN_NAME=:DOMAIN_NAME AND DOMAIN_TYPE=:DOMAIN_TYPE");
            //删除sql
            StringBuffer delsql = new StringBuffer();
            delsql.append("delete from dns_domain_record " +
                    "where DOMAIN_NAME=:DOMAIN_NAME AND TYPE=:TYPE and DOMAIN_TYPE=:DOMAIN_TYPE");
            StringBuffer delsql2 = new StringBuffer();
            delsql2.append("delete from dns_domain_record " +
                    "where DOMAIN_NAME=:DOMAIN_NAME AND DOMAIN_TYPE=:DOMAIN_TYPE");
            //日志sql
            StringBuffer insetDomainLog = new StringBuffer();
            insetDomainLog.append("INSERT INTO dns_domain_record_log (LOG_ID,DOMAIN_NAME,DOMAIN_TYPE,OPERATE_TYPE,TYPE,UPDATE_TIME,REMARK)");
            insetDomainLog.append(" values (:LOG_ID,:DOMAIN_NAME,:DOMAIN_TYPE,:OPERATE_TYPE,:TYPE,:UPDATE_TIME,:REMARK)");

            String delInfoSql = "delete from dns_domain_info where CUST_ID=:CUST_ID and DOMAIN_NAME=:DOMAIN_NAME and DOMAIN_TYPE=:DOMAIN_TYPE";
            String insetInfoLogSql = "INSERT INTO dns_domain_info_log (LOG_ID,CUST_ID,PARENT_DOMAIN_NAME,OPERATE_TYPE,DOMAIN_TYPE,DOMAIN_NAME,UPDATE_TIME,REAMRK)" +
                    " values (:LOG_ID,:CUST_ID,:PARENT_DOMAIN_NAME,:OPERATE_TYPE,:DOMAIN_TYPE,:DOMAIN_NAME,:UPDATE_TIME,:REMARK)";

            MapSqlParameterSource param =null;
            for(int i=0;i<domains.size();i++){
                JSONObject domain = domains.getJSONObject(i);
                param = new MapSqlParameterSource();
                param.addValue("CUST_ID",userJSON.get("name"));
                param.addValue("DOMAIN_NAME",domain.get("domainName"));
                param.addValue("DOMAIN_TYPE",domain.get("domainType"));
                param.addValue("LOG_ID", logId);
                param.addValue("OPERATE_TYPE", "D");
                param.addValue("UPDATE_TIME",DateUtil.getCurrentTime() );
                param.addValue("REMARK","删除域名");

                if (domain.containsKey("record")){
                    JSONArray types = domain.getJSONArray("record");
                    if (types != null && types.size()>0){
                        for (int j=0;j<types.size();j++){
                            param.addValue("TYPE",types.getJSONObject(j).get("type"));
                            daoManager.update(delsql.toString(),param);
                            param.addValue("REMARK","删除域名记录");
                            daoManager.update(insetDomainLog.toString(), param);
                        }
                    }
                }else{
                    daoManager.update(delInfoSql.toString(),param);
                    List<Map<String,Object>> list = daoManager.queryForList(querySql.toString(),param);
                    if (list != null && list.size()>0){
                        param.addValue("PARENT_DOMAIN_NAME",list.get(0).get("PARENT_DOMAIN_NAME"));
                        //记录日志
                        daoManager.update(insetInfoLogSql.toString(), param);
                    }
                    List<Map<String,Object>> domtypeList = daoManager.queryForList(querySql2.toString(),param);
                    int a = daoManager.update(delsql2.toString(),param);
                    if (a>0){
                        //日志记录需要先查询出来。以便获取数据
                        for (Map<String,Object> mm : domtypeList){
                            param.addValue("TYPE",mm.get("TYPE"));
                            daoManager.update(insetDomainLog.toString(),param);
                        }
                    }
                }
            }
        }catch(Exception e){
            e.printStackTrace();
            return "删除异常";
        }
        return "删除成功";
    }

    /**
     * 查询指定订单的某一省份下的返回结果
     * @param param
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> getOrderDetailByPro(Map param) throws Exception {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT DOMAIN_NAME,RES,TYPE,");
        sql.append(" IF(B.AREA_CODE=-1,'全市',(SELECT AREA_NAME  ");
        sql.append("  FROM  TD_M_CHINA_AREA m   WHERE   M.AREA_CODE = B.AREA_CODE AND M.AREA_LEVEL='2'  AND  m.province_code = b.province_code )) AREA_NAME,");
        sql.append("(SELECT   AREA_NAME  FROM TD_M_CHINA_AREA N  WHERE N.AREA_LEVEL='1' AND  N.PROVINCE_CODE = B.PROVINCE_CODE) PROVINCE_NAME ,");
        //地市、区域编码
        sql.append(" AREA_CODE,PROVINCE_CODE");
        sql.append(" FROM dns_order_detail B ");
        sql.append(" WHERE B.ORDER_ID=:ORDER_ID");
        sql.append(" AND PROVINCE_CODE=:PROVINCE_CODE");
        List<Map<String, Object>> result= daoManager.queryForList(sql.toString(),param);
        return result;
    }
    /**
     * 校验订单下发的域名数量是否超过限制数量
     * @param param
     * @return
     */
    @Override
    public boolean checkOrderNum(Map param){
        boolean flag = false;
        try{
            String usersql = "select RESOLV_COUNT,FLUSH_COUNT from dns_customer_info where CUST_ID=:CUST_ID";
            List<Map<String,Object>> userlist = daoManager.queryForList(usersql,param);
            Integer num = 0;
            if(DnsTask.TASK_TYPE_RES.equals(param.get("TASK_TYPE")+"")) {//域名解析
                num = (Integer) userlist.get(0).get("RESOLV_COUNT");
            }else {
//                num = (Integer) userlist.get(0).get("FLUSH_COUNT");
                num = 1;
            }
            String[] names = ((String)param.get("DOMAIN_NAME")).split(";");
            if (num < names.length)
                flag = true;
        }catch (Exception e){
        }finally {
            return flag;
        }
    }
    /**
     * 查询时有否有对应类型在途任务的订单.
     * @param param
     * @return
     * @throws Exception
     */
    @Override
    public boolean checkHasTradeOrder(Map param){
        boolean b = true;
        try{
            StringBuffer sql = new StringBuffer();
            sql.append(" select count(1) cnt from DNS_ORDER a  where a.cust_id = :CUST_ID ");
            sql.append(" and task_type=:TASK_TYPE");
            sql.append(" and task_status in (:TASK_STATUS)");
            //下发中、未执行、执行中的订单状态
            List statusList= Arrays.asList
                    (DnsTask.TASK_ISSUEING,DnsTask.TASK_NOT_EXECUTE,DnsTask.TASK_EXECUTING);
            param.put("TASK_STATUS",statusList);

            long count=0;
            List<Map<String,Object>>  resultList= daoManager.queryForList(sql.toString(),param);
            //因为List中只有一个元素，所以取第一个元素即可
            Map map =resultList.get(0);
            count= (long) map.get("cnt");
            b = count==0?false:true;
        }catch (Exception e){

        }finally {
            return b;
        }
    }
    /**
     * 添加域名解析订单记录
     * @param queryDnsJson
     * @return
     */
    @Override
    public String addDnsResultTask(String queryDnsJson,String attribution) throws Exception {
        //记录订单任务到订单表
        StringBuffer  ordersql  = new StringBuffer();
        ordersql.append(" insert into DNS_ORDER (ORDER_ID,CUST_ID,DOMAIN_NAME,AREA_CODE,CLIENT_IP,TASK_TYPE,CREATE_DATE,UPDATE_TIME,TASK_STATUS,STATUS_DESC,TASK_DESC,DNS_TYPE,ATTRIBUTION)");
        ordersql.append(" values (:ORDER_ID,:CUST_ID,:DOMAIN_NAME,:AREA_CODE,:CLIENT_IP,:TASK_TYPE,:CREATE_DATE,:UPDATE_TIME ,:TASK_STATUS,:STATUS_DESC,:TASK_DESC,:DNS_TYPE,:ATTRIBUTION)");

        Date now =DateUtil.getCurrentTime();
        //将JSON 数据 转换为JavaBean
        QueryDns queryDns = JSON.parseObject(queryDnsJson, new TypeReference<QueryDns>()
        {});
        //订单ID
        User user =queryDns.getUser();
        //客户编码
        String custId=user.getName();
        //客户端防的IP地址
        String clientIp= getRemoteAddr(request);
        Query query = queryDns.getQuery();
        String orderId=query.getWorkNum();
        Domain domain =query.getDomain();
        //预解析域名名称
        String domainName=domain.getName();
        //域名解析类型
        String type= domain.getType();
        String areaList =domain.getAreaCode();
        MapSqlParameterSource param = new MapSqlParameterSource();
        param.addValue("ORDER_ID",orderId);
        param.addValue("CUST_ID",custId);
        param.addValue("AREA_CODE",areaList);

        param.addValue("CLIENT_IP",clientIp);
        //任务类型
        param.addValue("TASK_TYPE",DnsTask.TASK_TYPE_RES);
        param.addValue("CREATE_DATE",now);
        param.addValue("UPDATE_TIME",now);
        //任务状态: 下发中.
        param.addValue("TASK_STATUS",DnsTask.TASK_ISSUEING);
        //任务状态描述
        param.addValue("STATUS_DESC","任务下发中");
        //任务描述
        param.addValue("TASK_DESC","域名解析");
        //DNS 解析类型
        param.addValue("DNS_TYPE",domain.getType());
        //归属那个清除缓存
        param.addValue("ATTRIBUTION",attribution);
        param.addValue("DOMAIN_NAME",domainName);
        daoManager.update(ordersql.toString(),param);
        return "成功";
    }
    /**
     * 下发清除缓存任务记录
     * @param cleanCacheJson
     * @return
     */
    @Override
    public String addCleanDomainCacheTask(String cleanCacheJson) throws  Exception {

        //记录订单任务到订单表
        StringBuffer  ordersql  = new StringBuffer();
        ordersql.append(" insert into DNS_ORDER (ORDER_ID,CUST_ID,DOMAIN_NAME,AREA_CODE,CLIENT_IP,TASK_TYPE,CREATE_DATE,UPDATE_TIME,TASK_STATUS,STATUS_DESC,TASK_DESC)");
        ordersql.append(" values (:ORDER_ID,:CUST_ID,:DOMAIN_NAME,:AREA_CODE,:CLIENT_IP,:TASK_TYPE,:CREATE_DATE,:UPDATE_TIME ,:TASK_STATUS,:STATUS_DESC,:TASK_DESC)");

        Date now =DateUtil.getCurrentTime();
        //将JSON 数据 转换为JavaBean
        CleanCache cleanCache = JSON.parseObject(cleanCacheJson, new
                TypeReference<CleanCache>() {});
        User user =cleanCache.getUser();
        //客户编码
        String custId=user.getName();
        //客户端防的IP地址
        String clientIp= getRemoteAddr(request);
        CleanTask cleanTask = cleanCache.getCleanTask();
        String  domainName =cleanTask.getDomain();
        String areaList=cleanTask.getAreaCode();
        //域名解析类型
        MapSqlParameterSource param = new MapSqlParameterSource();
        param.addValue("CUST_ID",custId);

        param.addValue("AREA_CODE",areaList);

        param.addValue("CLIENT_IP",clientIp);
        //任务类型
        param.addValue("TASK_TYPE",DnsTask.TASK_TYPE_CLEAN);
        param.addValue("CREATE_DATE",now);
        param.addValue("UPDATE_TIME",now);
        //任务状态:下发中
        param.addValue("TASK_STATUS",DnsTask.TASK_ISSUEING);
        //任务状态描述
        param.addValue("STATUS_DESC","任务执行中");
        //任务描述
        param.addValue("TASK_DESC","缓存刷新");
        //订单ID
        String orderId= getOrderId();
        param.addValue("ORDER_ID",orderId);
        param.addValue("DOMAIN_NAME",domainName);
        daoManager.update(ordersql.toString(),param);
        //更新成功，返回订单编号
        return orderId;
    }
    /**
     * 根据省份组合订单信息
     * 因每次下发的任务只有一种类型的订单信息，所有根据省份分组即可.
     * @param param
     * @param pagination
     * @return
     */
    @Override
    public IDataset getOrderProList(Map param, Pagination pagination) throws Exception {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT  PROVINCE_CODE , ");
        sql.append(" (SELECT  AREA_NAME  FROM TD_M_CHINA_AREA N WHERE N.AREA_LEVEL='1' AND N.PROVINCE_CODE = B.PROVINCE_CODE) PROVINCE_NAME ");
        sql.append(" FROM DNS_ORDER_DETAIL  B ");
        sql.append(" WHERE ORDER_ID=:ORDER_ID ");
        sql.append(" GROUP BY PROVINCE_CODE");
        IDataset result= daoManager.queryForListByPage(sql.toString(),param,pagination);
        return result;
    }
    /**
     * 判断订单是否下发
     * @param custId
     * @param orderId
     * @param domainName
     * @param type
     * @return
     * @throws Exception
     */
    @Override
    public String checkTaskIsList(String custId,String orderId,String domainName,String type) throws Exception{

        String sql = "SELECT ORDER_ID FROM dns_order " +
                "WHERE CUST_ID=:CUST_ID AND ATTRIBUTION=:ATTRIBUTION " +
                "AND DOMAIN_NAME=:DOMAIN_NAME AND DNS_TYPE=:DNS_TYPE";
        MapSqlParameterSource param = new MapSqlParameterSource();
        param.addValue("CUST_ID",custId);
        param.addValue("ATTRIBUTION",orderId);
        param.addValue("DOMAIN_NAME",domainName);
        param.addValue("DNS_TYPE",type);
        List<Map<String,Object>> list = daoManager.queryForList(sql,param);
        String result = null;
        if (list != null && list.size() > 0){
            result = (String)list.get(0).get("ORDER_ID");
        }
        return result;
    }
    /**
     * 根据订单id查询订单详细
     * @param orderId 订单id
     * @return
     */
    public List<Map<String, Object>> queryOrderDetailList(String orderId) throws Exception{
        String sql = "SELECT " +
                "(SELECT AREA_NAME FROM td_m_china_area WHERE AREA_CODE = d.PROVINCE_CODE) PROVINCE_NAME," +
                "(SELECT AREA_NAME FROM td_m_china_area WHERE AREA_CODE = IF(d.AREA_CODE=-1,d.PROVINCE_CODE,d.AREA_CODE) ) AREA_NAME," +
                "d.ORDER_ID," +
                "d.DOMAIN_NAME," +
                "d.PROVINCE_CODE," +
                "d.AREA_CODE," +
                "d.REMARK," +
                "d.EXECU_TIME," +
                "d.RES," +
                "d.TYPE," +
                "d.UPDATE_TIME " +
                "FROM dns_order_detail d WHERE d.ORDER_ID = :ORDER_ID ";
        MapSqlParameterSource param = new MapSqlParameterSource();
        param.addValue("ORDER_ID",orderId);
        List<Map<String, java.lang.Object>> list = daoManager.queryForList(sql,param);
        if (list != null && list.size() <=0)
            list = null;
        return list;
    }
    @Override
    public List<Map<String,Object>> checkDeleteTypes(String domainName, String domainType, List<String> types) {
        StringBuffer sql = new StringBuffer();
        sql.append("select type from dns_domain_record where DOMAIN_NAME=:DOMAIN_NAME ");
        sql.append("and DOMAIN_TYPE =:DOMAIN_TYPE ");
        sql.append("and TYPE not in (:TYPE) ");
        MapSqlParameterSource param = new MapSqlParameterSource();
        param.addValue("DOMAIN_NAME",domainName);
        param.addValue("DOMAIN_TYPE",domainType);
        param.addValue("TYPE",types);
        List<Map<String,Object>> data = daoManager.queryForList(sql.toString(),param);
        return data;
    }
    /**
     * 根据订单id查询订单
     * @param orderId 订单id
     * @return
     */
    @Override
    public Map<String,Object> queryOrderById(String orderId) throws Exception{
        String sql = "select * from dns_order where ORDER_ID=:ORDER_ID";
        MapSqlParameterSource param = new MapSqlParameterSource();
        param.addValue("ORDER_ID",orderId);
        List<Map<String,Object>> list = daoManager.queryForList(sql,param);
        if (list != null && list.size() <= 0){
            return  null;
        }
        return list.get(0);
    }
    /**
     * 分页查询订单返回结果
     * @param param
     * @param pagination
     * @return
     * @throws Exception
     */
    public IDataset getOrderDatilPage(Map param, Pagination pagination) throws Exception{
        String sql = "SELECT " +
                "d.ORDER_ID," +
                "d.DOMAIN_NAME," +
                "d.PROVINCE_CODE," +
                "(SELECT AREA_NAME FROM td_m_china_area WHERE AREA_CODE=d.PROVINCE_CODE) PROVINCE_NAME," +
                "d.AREA_CODE," +
                "IF (d.PROVINCE_CODE = d.AREA_CODE,''," +
                "(SELECT AREA_NAME FROM td_m_china_area WHERE AREA_CODE = d.AREA_CODE) ) AREA_NAME,d.REMARK," +
                "d.EXECU_TIME," +
                "d.RES,d.TYPE,d.UPDATE_TIME FROM dns_order_detail d WHERE d.ORDER_ID = :ORDER_ID";
        IDataset result = daoManager.queryForListByPage(sql,param,pagination);
        return result;
    }
}